package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Fillerhazard;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FillerhazardDAO")
@Transactional
public class FillerhazardDAOImpl extends AbstractJpaDao<Fillerhazard> implements FillerhazardDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Fillerhazard.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FillerhazardDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Fillerhazard findFillerhazardById(Integer id) throws DataAccessException {

		return findFillerhazardById(id, -1, -1);
	}

	@Transactional
	public Fillerhazard findFillerhazardById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerhazardById", startResult, maxRows, id);
			return (Fillerhazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByName(String name) throws DataAccessException {

		return findFillerhazardByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByName", startResult, maxRows, name);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByNameContaining(String name) throws DataAccessException {

		return findFillerhazardByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByMin(BigDecimal min) throws DataAccessException {

		return findFillerhazardByMin(min, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByMin(BigDecimal min, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByMin", startResult, maxRows, min);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByMax(BigDecimal max) throws DataAccessException {

		return findFillerhazardByMax(max, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByMax(BigDecimal max, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByMax", startResult, maxRows, max);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByDegree(String degree) throws DataAccessException {

		return findFillerhazardByDegree(degree, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByDegree(String degree, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByDegree", startResult, maxRows, degree);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByDegreeContaining(String degree) throws DataAccessException {

		return findFillerhazardByDegreeContaining(degree, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByDegreeContaining(String degree, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByDegreeContaining", startResult, maxRows, degree);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByDescription(String description) throws DataAccessException {

		return findFillerhazardByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardByDescriptionContaining(String description) throws DataAccessException {

		return findFillerhazardByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerhazardByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@Transactional
	public Fillerhazard findFillerhazardByPrimaryKey(Integer id) throws DataAccessException {
		return findFillerhazardByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Fillerhazard findFillerhazardByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerhazardByPrimaryKey", startResult, maxRows, id);
			return (Fillerhazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerhazardsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillerhazard> findAllFillerhazards() throws DataAccessException {

		return findAllFillerhazards(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findAllFillerhazards(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFillerhazards", startResult, maxRows);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Fillerhazard> findFillerhazardsByFiller(Integer fillerId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFillerhazardsByFiller", start, max, fillerId);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Fillerhazard> findFillerhazardsByHazard(Integer hazardId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFillerhazardsByHazard", start, max, hazardId);
		return new LinkedHashSet<Fillerhazard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerhazard> findFillerhazardByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Fillerhazard>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Fillerhazard> findFillerhazardsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Fillerhazard.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Fillerhazard> fillerhazards = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return fillerhazards;
	}

	@Transactional
	public Fillerhazard findFillerhazardByPhysicalPrimaryKey(Integer fillerId, Integer hazardId, String degree)
			throws DataAccessException {
		return findFillerhazardByPhysicalPrimaryKey(fillerId, hazardId, degree, -1, -1);
	}

	@Transactional
	public Fillerhazard findFillerhazardByPhysicalPrimaryKey(Integer fillerId, Integer hazardId, String degree,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerhazardByPhyscialPrimaryKey", startResult, maxRows, fillerId,
					hazardId, degree);
			return (Fillerhazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Fillerhazard entity) {
		return true;
	}
}
